package com.justnow.offer;

/**
 * @author justnow
 * Created on 2020-08-14
 * Description
 */
public class Solution11 {

    /**
     * 方法一：找到第一个后面小于前面的元素
     * @param numbers
     * @return
     */
    public int minArray(int[] numbers) {
        int length = numbers.length;
        int min = numbers[0];
        for (int i = 1; i < length; i++) {
            if (numbers[i] < min)
                min = numbers[i];
        }
        return min;
    }

    /**
     * 方法二，使用二分查找，每次与最右边一个元素判断
     * @param numbers
     * @return
     */
    public int minArray2(int[] numbers) {
        int len = numbers.length;
        int low = 0, high = len - 1;
        while (low < high) {
            int pivot = low + (high - low) / 2;
            if (numbers[pivot] < high) {
                high = pivot;
            } else if (numbers[pivot] > high) {
                low = pivot + 1;
            } else {
                high--;
            }
        }

        return numbers[low];

    }
}
